package com.ml.doctor.activity;

import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.widget.RadioGroup;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.google.gson.reflect.TypeToken;
import com.ml.doctor.R;
import com.ml.doctor.bean.ChatDataBean;
import com.ml.doctor.network.NetworkApi;
import com.ml.doctor.network.NetworkManager;
import com.ml.doctor.utils.TimeFormatter;
import com.ml.doctor.utils.ToastUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ChatActivity extends BaseActivity {

    @BindView(R.id.linechat)
    LineChart lineChartView;
    @BindView(R.id.rg)
    RadioGroup rg;
    long time;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);
        ButterKnife.bind(this);
        time = 7 * 24 * 60 * 60 * 1000l;
        getListData();
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId) {
                    case R.id.rb_zhou:
                        time = 7 * 24 * 60 * 60 * 1000l;
                        getListData();
                        break;
                    case R.id.rb_ji:
                        time = 30 * 3 * 24 * 60 * 60 * 1000l;
                        getListData();
                        break;
                    case R.id.rb_yue:
                        time = 30 * 24 * 60 * 60 * 1000l;
                        getListData();
                        break;
                    case R.id.rb_nian:
                        time = 365 * 24 * 60 * 60 * 1000l;
                        getListData();
                        break;
                }
            }
        });
    }


    private void getListData() {
        Map<String, String> map = new HashMap<>();
        map.put("bid", getIntParam("bid") + "");
        map.put("temp", getIntParam("temp") + "");
        map.put("starttime", System.currentTimeMillis() - time + "");
        map.put("endtime", System.currentTimeMillis() + "");
        NetworkManager.getInstance().getResultClass(NetworkApi.BasicUrl + "/br/cl_data", map, new TypeToken<List<ChatDataBean>>() {
        }.getType(), new NetworkManager.SuccessCallback<List<ChatDataBean>>() {
            @Override
            public void onSuccess(List<ChatDataBean> response) {
                initPointValues(getIntParam("temp"), response);
            }
        }, new NetworkManager.FailedCallback() {
            @Override
            public void onFailed(String message) {
                lineChartView.setNoDataText("暂无数据");
                ToastUtil.showShort(ChatActivity.this, message);
            }
        });
    }

    private void setChart(int min) {

        lineChartView.getDescription().setEnabled(false);
        lineChartView.setTouchEnabled(true);
        lineChartView.setDragEnabled(true);
        lineChartView.setScaleEnabled(true);
        lineChartView.setScaleYEnabled(false);
        lineChartView.setExtraLeftOffset(40);
        lineChartView.setExtraRightOffset(10);
        lineChartView.setMaxVisibleValueCount(10);
        lineChartView.setNoDataText("暂无数据");
        XAxis xAxis = lineChartView.getXAxis();
        //绘制底部的X轴
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        //启用X轴的网格虚线
        xAxis.setDrawGridLines(false);
        //缩放时候的粒度
        xAxis.setGranularity(1);
        xAxis.setTextSize(15f);
        //在可见范围只显示四个
        xAxis.setLabelCount(4);
        YAxis leftAxis = lineChartView.getAxisLeft();
        leftAxis.removeAllLimitLines(); // reset all limit lines to avoid overlapping lines
        leftAxis.resetAxisMaximum();
        leftAxis.resetAxisMinimum();
        leftAxis.setAxisMinimum(min);
        leftAxis.setTextSize(15f);
        leftAxis.setDrawGridLines(false);//不启用y轴的参考线
        //启用零线
        leftAxis.setDrawZeroLine(false);

        //绘制警戒线在绘制数据之后
        leftAxis.setDrawLimitLinesBehindData(false);

        //禁用右边的Y轴
        lineChartView.getAxisRight().setEnabled(false);
        //动画时间
        lineChartView.animateX(1000);
    }

    private void setData(ArrayList<Entry> values, ArrayList<Entry> values2, ArrayList<Long> times) {

        LineDataSet set1;
        LineDataSet set2 = null;
        set1 = new LineDataSet(values, "");
        set1.setDrawIcons(false);
        set1.enableDashedHighlightLine(10f, 0f, 0f);
        set1.setHighLightColor(Color.rgb(244, 117, 117));
        set1.setValueTextSize(18f);
        set1.setColor(getResources().getColor(R.color.main_color));
        set1.setCircleColor(getResources().getColor(R.color.main_color));
        set1.setLineWidth(2f);
        set1.setCircleRadius(4f);
        set1.setDrawCircleHole(true);
        set1.setCircleHoleRadius(4f);
        set1.setFormLineWidth(0f);
        set1.setFormLineDashEffect(new DashPathEffect(new float[]{0f, 0f}, 0f));
        set1.setFormSize(0f);
        set1.setDrawFilled(false);
        if (values.size() <= 3)
            set1.setMode(LineDataSet.Mode.LINEAR);
        else
            set1.setMode(LineDataSet.Mode.CUBIC_BEZIER);
        if (values2 != null) {
            set2 = new LineDataSet(values2, "");
            set2.setDrawIcons(false);
            set2.enableDashedHighlightLine(10f, 0f, 0f);
            set2.setHighLightColor(Color.rgb(244, 117, 117));
            set2.setValueTextSize(18f);
            set2.setColor(getResources().getColor(R.color.gray_line));
            set2.setCircleColor(getResources().getColor(R.color.gray_line));
            set2.setLineWidth(2f);
            set2.setCircleRadius(4f);
            set2.setDrawCircleHole(true);
            set2.setCircleHoleRadius(4f);
            set2.setFormLineWidth(0f);
            set2.setFormLineDashEffect(new DashPathEffect(new float[]{0f, 0f}, 0f));
            set2.setFormSize(0f);
            set2.setDrawFilled(false);
            if (values2.size() <= 3)
                set2.setMode(LineDataSet.Mode.LINEAR);
            else
                set2.setMode(LineDataSet.Mode.CUBIC_BEZIER);
//            set2.setMode(LineDataSet.Mode.LINEAR);
        }

//        ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
//        dataSets.add(set1);
        LineData lineData = null;
        if (set2 != null) {
            lineData = new LineData(set1, set2);
        } else {
            lineData = new LineData(set1);
        }
        lineChartView.setData(lineData);
        lineChartView.getXAxis().setValueFormatter(new TimeFormatter(times));
        lineChartView.invalidate();
    }

    private void initPointValues(int temp, List<ChatDataBean> list) {
        ArrayList<Entry> values = new ArrayList<Entry>();
        ArrayList<Entry> values2 = new ArrayList<Entry>();
        ArrayList<Long> times = new ArrayList<Long>();
        switch (temp) {
            case 1:
                setTopTitle("体温表");
                setChart(30);
                for (int i = 0; i < list.size(); i++) {
                    values.add(new Entry(i, list.get(i).temper_ature));
                }
                for (int i = 0; i < list.size(); i++) {
                    times.add(list.get(i).time);
                }
                setData(values, null, times);
                break;
            case 2:
                setTopTitle("血压表");
                setChart(50);
                for (int i = 0; i < list.size(); i++) {
                    values.add(new Entry(i, list.get(i).high_pressure));
                }
                for (int i = 0; i < list.size(); i++) {
                    values2.add(new Entry(i, list.get(i).low_pressure));
                }
                for (int i = 0; i < list.size(); i++) {
                    times.add(list.get(i).time);
                }
                setData(values, values2, times);
                break;
            case 3:
                setTopTitle("心率表");
                setChart(34);
                for (int i = 0; i < list.size(); i++) {
                    values.add(new Entry(i, list.get(i).low_pressure));
                }
                for (int i = 0; i < list.size(); i++) {
                    times.add(list.get(i).time);
                }
                setData(values, null, times);
                break;
            case 4:
                setTopTitle("血糖表");
                setChart(0);
                for (int i = 0; i < list.size(); i++) {
                    values.add(new Entry(i, list.get(i).blood_sugar));
                }
                for (int i = 0; i < list.size(); i++) {
                    times.add(list.get(i).time);
                }
                setData(values, null, times);
                break;
            case 5:
                setTopTitle("血氧表");
                setChart(90);
                for (int i = 0; i < list.size(); i++) {
                    values.add(new Entry(i, list.get(i).blood_oxygen));
                }
                for (int i = 0; i < list.size(); i++) {
                    times.add(list.get(i).time);
                }
                setData(values, null, times);
                break;
            case 6:
                setTopTitle("脉搏表");
                setChart(30);
                for (int i = 0; i < list.size(); i++) {
                    values.add(new Entry(i, list.get(i).pulse));
                }
                for (int i = 0; i < list.size(); i++) {
                    times.add(list.get(i).time);
                }
                setData(values, null, times);
                break;
            case 7:
                setTopTitle("胆固醇表");
                setChart(0);
                for (int i = 0; i < list.size(); i++) {
                    values.add(new Entry(i, list.get(i).cholesterol));
                }
                for (int i = 0; i < list.size(); i++) {
                    times.add(list.get(i).time);
                }
                setData(values, null, times);
                break;
            case 8:
                setTopTitle("尿酸表");
                setChart(70);
                for (int i = 0; i < list.size(); i++) {
                    values.add(new Entry(i, list.get(i).uric_acid));
                }
                for (int i = 0; i < list.size(); i++) {
                    times.add(list.get(i).time);
                }
                setData(values, null, times);
                break;
        }
    }
}
